package com.corechina.apex_osswords.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.corechina.apex_osswords.common.Response;
import com.corechina.apex_osswords.common.ShiroUtils;
import com.corechina.apex_osswords.domain.dao.SysMenuMapper;
import com.corechina.apex_osswords.domain.dao.SysUserGroupsMenuMapper;
import com.corechina.apex_osswords.domain.entity.SysMenu;
import com.corechina.apex_osswords.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author cc
 * @since 2022-03-07
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysUserGroupsMenuMapper sysUserGroupsMenuMapper;

    @Override
    public Response add(SysMenu sysMenu) {
        sysMenuMapper.insert(sysMenu);
        return Response.ok();
    }

    @Override
    public Response update(SysMenu sysMenu) {
        sysMenuMapper.updateById(sysMenu);
        return Response.ok();
    }

    @Override
    @Transactional
    public Response del(Integer id) {
        sysUserGroupsMenuMapper.deleteByMenuId(id);
        sysMenuMapper.deleteById(id);
        return Response.ok();
    }

    @Override
    public Response getOne(Integer id) {
        return Response.ok(sysMenuMapper.selectById(id));
    }

    @Override
    public Response getMenuAllList() {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc(Arrays.asList("parent_id", "sort"));
        return Response.ok(sysMenuMapper.selectList(wrapper));
    }

    @Override
    public List<SysMenu> getMenuByParentId(Integer parentId, String type) {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", parentId);
        if (type != null) {
            wrapper.eq(type, type);
        }
        return sysMenuMapper.selectList(wrapper);
    }

    @Override
    public Response getMenuByUserId() {
        return Response.ok(sysMenuMapper.queryMenuByUserId(ShiroUtils.getUserId()));
    }
}
